package com.axzh.interview.leetcode;

public class SlidingWindowMaximum {

	public static void main(String[] args) {
		int[] nums = {1,-1};
		int k = 1;
		SlidingWindowMaximum maximum = new SlidingWindowMaximum();
		int result[]=maximum.maxSlidingWindow(nums, k);
		for(int i=0;i<result.length;i++) {
			System.out.print(result[i]+" ");
		}
	}
	
	/*public int[] maxSlidingWindow(int[] nums, int k) {
		if(nums==null || k<0 || nums.length-k<0) {
			return null;
		}
		if(nums.length ==0 && k==0) {
			return nums;
		}
		int[] result = new int[nums.length-k+1];
		LinkedList<Integer> list = new LinkedList<>();
		for(int i=0;i<nums.length;i++) {
			int tmp = nums[i];
			while(!list.isEmpty() && list.getLast()< tmp) {
				list.removeLast();
			}
			list.add(tmp);
			
			if(i<k-1)
				continue;
			result[i-k+1] = list.get(0);
			if(nums[i-k+1] == result[i-k+1]){
				list.removeFirst();
			}
		}
		return result;
	}*/
	
	public int[] maxSlidingWindow(int[] nums, int k) {
		//int[] a = new int[k];
		if(nums==null || k<0 || nums.length-k<0) {
			return null;
		}
		if(nums.length ==0 && k==0) {
			return nums;
		}
		int[] result = new int[nums.length-k+1];
		for(int i=0;i<nums.length;i++) {
			if((i+k)<=nums.length) {
				int max = nums[i];
				for(int j=0;j<k;j++) {
					int tmp_1 = nums[i+j];
					if(max<tmp_1) {
						int tmp = max;
						max = tmp_1;
						tmp_1=tmp;
					}
				}
				result[i]=max;
			}
		}
		return result;
	}

}
